import {PrismaClient} from '@prisma/client';
import {z} from 'zod';

// Scene 模型的 Zod 校验规则
const SceneSchema = z.object({
  id: z.string().uuid(),
  imageUrl: z.string().url(),
  title: z.string(),
  description: z.string(),
});

const CreateSceneInput = SceneSchema.omit({ id: true });
const UpdateSceneInput = CreateSceneInput.partial();

// Food 模型的 Zod 校验规则
const FoodSchema = z.object({
  id: z.string().uuid(),
  imageUrl: z.string().url(),
  title: z.string().min(1).max(255),
  type: z.number().int().min(0),
});

const CreateFoodInput = FoodSchema.omit({ id: true });
const UpdateFoodInput = CreateFoodInput.partial();

// Entertainment 模型的 Zod 校验规则
const EntertainmentSchema = z.object({
  id: z.string().uuid(),
  imageUrl: z.string().url(),
  title: z.string(),
  description: z.string(),
});

const CreateEntertainmentInput = EntertainmentSchema.omit({ id: true });
const UpdateEntertainmentInput = CreateEntertainmentInput.partial();

const prisma = new PrismaClient();

// Scene 相关服务
export const createScene = async (input: unknown) => {
  try {
    const validatedInput = CreateSceneInput.parse(input);
    return await prisma.scene.create({
      data: validatedInput,
    });
  } catch (error) {
    console.error('创建景点时出错:', error);
    throw error;
  }
};

export async function getScenes() {
  try {
    return await prisma.scene.findMany();
  } catch (error) {
    console.error('获取景点数据时出错:', error);
    throw error;
  }
}

export const getSceneById = async (id: string) => {
  try {
    return await prisma.scene.findUnique({
      where: {id},
    });
  } catch (error) {
    console.error('获取景点时出错:', error);
    throw error;
  }
};

export const updateScene = async (id: string, input: unknown) => {
  try {
    const validatedInput = UpdateSceneInput.parse(input);
    return await prisma.scene.update({
      where: {id},
      data: validatedInput,
    });
  } catch (error) {
    console.error('更新景点时出错:', error);
    throw error;
  }
};

export const deleteScene = async (id: string) => {
  try {
    return await prisma.scene.delete({
      where: {id},
    });
  } catch (error) {
    console.error('删除景点时出错:', error);
    throw error;
  }
};

// Food 相关服务
export const createFood = async (input: unknown) => {
  try {
    const validatedInput = CreateFoodInput.parse(input);
    return await prisma.food.create({
      data: validatedInput,
    });
  } catch (error) {
    console.error('创建食物时出错:', error);
    throw error;
  }
};

export async function getFoods() {
  try {
    return await prisma.food.findMany();
  } catch (error) {
    console.error('获取食物数据时出错:', error);
    throw error;
  }
}

export const getFoodById = async (id: string) => {
  try {
    return await prisma.food.findUnique({
      where: {id},
    });
  } catch (error) {
    console.error('获取食物时出错:', error);
    throw error;
  }
};

export const updateFood = async (id: string, input: unknown) => {
  try {
    const validatedInput = UpdateFoodInput.parse(input);
    return await prisma.food.update({
      where: {id},
      data: validatedInput,
    });
  } catch (error) {
    console.error('更新食物时出错:', error);
    throw error;
  }
};

export const deleteFood = async (id: string) => {
  try {
    return await prisma.food.delete({
      where: {id},
    });
  } catch (error) {
    console.error('删除食物时出错:', error);
    throw error;
  }
};

// Entertainment 相关服务
export const createEntertainment = async (input: unknown) => {
  try {
    const validatedInput = CreateEntertainmentInput.parse(input);
    return await prisma.entertainment.create({
      data: validatedInput,
    });
  } catch (error) {
    console.error('创建娱乐活动时出错:', error);
    throw error;
  }
};

export async function getEntertainments() {
  try {
    return await prisma.entertainment.findMany();
  } catch (error) {
    console.error('获取娱乐活动数据时出错:', error);
    throw error;
  }
}

export const getEntertainmentById = async (id: string) => {
  try {
    return await prisma.entertainment.findUnique({
      where: {id},
    });
  } catch (error) {
    console.error('获取娱乐活动时出错:', error);
    throw error;
  }
};

export const updateEntertainment = async (id: string, input: unknown) => {
  try {
    const validatedInput = UpdateEntertainmentInput.parse(input);
    return await prisma.entertainment.update({
      where: {id},
      data: validatedInput,
    });
  } catch (error) {
    console.error('更新娱乐活动时出错:', error);
    throw error;
  }
};

export const deleteEntertainment = async (id: string) => {
  try {
    return await prisma.entertainment.delete({
      where: {id},
    });
  } catch (error) {
    console.error('删除娱乐活动时出错:', error);
    throw error;
  }
};

